package com.hsylient.javase.chapter05;

import lombok.SneakyThrows;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;

public class B_MyFileInputStream {

    /**
     * 一次只能读取一个字节
     */
    @Test
    public void readFile() {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("D:\\IdeaProjects\\logger\\log\\JULCustomProperties.txt");
            System.out.println(fileInputStream.read());
            System.out.println(fileInputStream.read());
            System.out.println(fileInputStream.read());
            System.out.println(fileInputStream.read());
            System.out.println(fileInputStream.read());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 改进一次只能读取到一个字节，使用循环来读
     */
    @Test
    public void readCycle() {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("D:\\IdeaProjects\\logger\\log\\JULCustomProperties.txt");
            int i;
            while ((i = fileInputStream.read()) != -1) {
                System.out.println(i);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 下面的代码中主要是展示了，将外部文件读入到内存中
     * 注意read方法的参数为数组的时候，返回的是它读取到的字节的数量，所以在输出的时候，需要使用到这个值
     * 因为read方法在读取的时候采用的是覆盖的形式比如：数组的长度是4，目标是：abcdef 第一次读取到abcd
     * 第二次是就是ef将ab覆盖掉 得到efcd 所以字节转字符串要使用read的方法的返回值，而不是数组的有效元素个数
     */
    @Test
    public void read2Array() {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("D:\\IdeaProjects\\logger\\log\\JULCustomProperties.txt");
            byte[] bytes = new byte[4];
            int byteLen;
            while ((byteLen = fileInputStream.read(bytes)) != -1) {
                System.out.print(new String(bytes, 0,byteLen));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 输入流中剩余的字节数，在没有任何读取的时候，available()显示的是文件的字节大小
     * skip()跳过指定的字节数不读
     * 下例中的方法不可用于大文件的读取
     *
     */
    @SneakyThrows
    @Test
    public void testAvailable() {
        FileInputStream fileInputStream = new FileInputStream("D:\\IdeaProjects\\logger\\log\\xx.txt");
        System.out.println(fileInputStream.available());
        fileInputStream.skip(3L);
        byte[] bytes = new byte[3];
        while (fileInputStream.available() != 0) {
            int readLen = fileInputStream.read(bytes);
            System.out.print(new String(bytes, 0, readLen));
        }
    }
}
